//
//  QueryDetailListVC.m
//  CJRailTransport
//
//  Created by Hank on 2017/11/29.
//  Copyright © 2017年 amplity. All rights reserved.
//

#import "QueryDetailListVC.h"
#import "AssetStatesCell.h"
#import "HypotacticWestCell.h"
#import "HypotacticEastCell.h"
#import "HypotacticBracketCell.h"
#import "QueryDetailListModel.h"
#import "QueryContainerMassLossDetailVC.h"
#import "UIView+Expand.h"
//#import "DamagedConditionVC.h"
//#import "ProductDetailVC.h"

@interface QueryDetailListVC ()
// 数据 Model
@property (strong, nonatomic) QueryDetailListModel *queryDetailListModel;

@end

@implementation QueryDetailListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置界面属性
    [self setViewAttribute];
    // 注册Cell
    [self registerCells];
    // 设置tableView headView
    [self loadTableHeadView];
    // 请求数据
//    [self requestData];
}

// 设置界面属性
- (void)setViewAttribute
{
    self.view.backgroundColor = RGB_255(215, 215, 215);
}

// 注册Cell
- (void)registerCells
{
    UINib *nib = [UINib nibWithNibName:[AssetStatesCell reuseIdentifier] bundle:nil];
    [_myTableView registerNib:nib forCellReuseIdentifier:[AssetStatesCell reuseIdentifier]];
    nib = [UINib nibWithNibName:[HypotacticWestCell reuseIdentifier] bundle:nil];
    [_myTableView registerNib:nib forCellReuseIdentifier:[HypotacticWestCell reuseIdentifier]];
    nib = [UINib nibWithNibName:[HypotacticEastCell reuseIdentifier] bundle:nil];
    [_myTableView registerNib:nib forCellReuseIdentifier:[HypotacticEastCell reuseIdentifier]];
    nib = [UINib nibWithNibName:[HypotacticBracketCell reuseIdentifier] bundle:nil];
    [_myTableView registerNib:nib forCellReuseIdentifier:[HypotacticBracketCell reuseIdentifier]];
}

#pragma mark - tableView headView
// 设置tableView headView
- (void)loadTableHeadView
{
    if (_index > 0) {
        // 设置圆角
        [self.containerNumberBg radiusView:3 backgroundColor:[UIColor clearColor] borderWidth:0.5 borderColor:[UIColor whiteColor]];
        _myTableView.tableHeaderView = self.tableHeadView;
    }else{
        _myTableView.tableHeaderView = nil;
    }
}

// 请求数据
- (void)requestData
{
    // 防止循环引用
    WEAK_REF(self);
    // 根据滑动的位置来取数据
    if (self.index == 0) {
        if ([self.target isEqualToString:@"2"]) {
            // 查询集装箱详细信息
            NSDictionary *info = @{@"vin":_vin};
            [[CSSManager getQueryOperator]postQueryContainer:info tag:self successBlock:^(id respondObject) {
                // 测试集装箱详细信息接口
    //            NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"QueryDetailListVC_Container" ofType:@"json"]];
    //            respondObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                QueryModel_Container *queryModel_Container = [[QueryModel_Container alloc] initWithDictionary:Infos error:nil];
                weak_self.detailInfoModel = queryModel_Container.retmarkobj.container;
                [weak_self.myTableView reloadData];
            } failureBlock:^(id respondObject) {
                // 解析返回错误信息
                ReturnErrorModel *returnErrorModel = [[ReturnErrorModel alloc] initWithDictionary:Infos error:nil];
                // 显示错误信息
                [MBProgressHUD showError:returnErrorModel.retmsg];
            }];
        }else{
            // 查询集装箱详细信息
            NSDictionary *info = @{@"vin":_vin};
            [[CSSManager getQueryOperator]postQueryStents:info tag:self successBlock:^(id respondObject) {
                // 测试集装箱详细信息接口
//                NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"QueryDetailListVC_Container" ofType:@"json"]];
//                respondObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                QueryDetailRackModel *queryDetailRackModel = [[QueryDetailRackModel alloc] initWithDictionary:Infos error:nil];
                weak_self.detailInfoModel = queryDetailRackModel.retmarkobj.rack;
                [weak_self.myTableView reloadData];
            } failureBlock:^(id respondObject) {
                // 解析返回错误信息
                //            ReturnErrorModel *returnErrorModel = [[ReturnErrorModel alloc] initWithDictionary:Infos error:nil];
                //            // 显示错误信息
                //            [MBProgressHUD showError:returnErrorModel.retmsg];
            }];
        }
    }else if (self.index == 1) {
        // 查询主从关系
        NSDictionary *info = @{@"vin":_vin,@"target": [NSString stringWithFormat:@"%ld",[_target integerValue] - 1]};
        [[CSSManager getQueryOperator] postQueryOwnerMember:info tag:self successBlock:^(id respondObject) {
            // 测试主从关系接口
//            NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"QueryDetailListVC" ofType:@"json"]];
//            respondObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            // 获取班列数据
            QueryDetailListModel *queryDetailListModel = [[QueryDetailListModel alloc] initWithDictionary:Infos error:nil];
            weak_self.queryDetailListModel = queryDetailListModel;
            [weak_self.myTableView reloadData];
        } failureBlock:^(id respondObject) {
            // 解析返回错误信息
            ReturnErrorModel *returnErrorModel = [[ReturnErrorModel alloc] initWithDictionary:Infos error:nil];
            // 显示错误信息
            [MBProgressHUD showError:returnErrorModel.retmsg];
        }];
    }
}

#pragma mark - TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_index > 0) {
        if ([_vin isEqualToString:@"1"] || [_vin isEqualToString:@"2"]) {
            return 1;
        }else{
            return 10;
        }
    }else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 防止循环引用
    WEAK_REF(self);
    
    if (self.index == 0) {
        AssetStatesCell *cell = [tableView dequeueReusableCellWithIdentifier:[AssetStatesCell reuseIdentifier] forIndexPath:indexPath];
        // 损坏情况
        [cell setDamagedConditionBlock:^{
            // 查询损坏情况
            QueryContainerMassLossDetailVC *queryContainerMassLossDetailVC = [[QueryContainerMassLossDetailVC alloc]init];
//            damagedConditionVC.vin = weak_self.vin;
//            damagedConditionVC.target = weak_self.target;
            [weak_self.navigationController pushViewController:queryContainerMassLossDetailVC animated:YES];
        }];
        [cell setData:self.detailInfoModel target:_target];
        return cell;
    }else{
        // 暂时测试用 1为西向  其他为东向
        if ([_vin isEqualToString:@"1"]) {
            HypotacticWestCell *cell = [tableView dequeueReusableCellWithIdentifier:[HypotacticWestCell reuseIdentifier] forIndexPath:indexPath];
            // 跳转到集装箱或支架，托盘的详细页面
            [cell setSkipDetailBlock:^(NSInteger tag ,NSString *vin){
                // tag 已修改因为从新设置集装箱 tag值降一位
    //            ProductDetailVC *productDetailVC = [[ProductDetailVC alloc]init];
    //            productDetailVC.vin = vin;
    //            productDetailVC.target = [NSString stringWithFormat:@"%ld",tag];
    //            [weak_self.navigationController pushViewController:productDetailVC animated:YES];
            }];
    //        [cell setData:self.queryDetailListModel vin:_vin];
            return cell;
        }else if ([_vin isEqualToString:@"2"]) {
            HypotacticEastCell *cell = [tableView dequeueReusableCellWithIdentifier:[HypotacticEastCell reuseIdentifier] forIndexPath:indexPath];
            // 跳转到集装箱或支架，托盘的详细页面
            [cell setSkipDetailBlock:^(NSInteger tag ,NSString *vin){
                // tag 已修改因为从新设置集装箱 tag值降一位
                //            ProductDetailVC *productDetailVC = [[ProductDetailVC alloc]init];
                //            productDetailVC.vin = vin;
                //            productDetailVC.target = [NSString stringWithFormat:@"%ld",tag];
                //            [weak_self.navigationController pushViewController:productDetailVC animated:YES];
            }];
            //        [cell setData:self.queryDetailListModel vin:_vin];
            return cell;
        }else{
            HypotacticBracketCell *cell = [tableView dequeueReusableCellWithIdentifier:[HypotacticBracketCell reuseIdentifier] forIndexPath:indexPath];
            cell.indexPath = indexPath;
            [cell setData:nil GPSStr:nil];
            return cell;
        }
    }
}

#pragma mark - TableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.index > 0) {
        if ([_vin isEqualToString:@"1"] || [_vin isEqualToString:@"2"]) {
            return 261;
        }else{
            return 88;
        }
    }else{
        return 284;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (self.index > 0 && ![_vin isEqualToString:@"1"] && ![_vin isEqualToString:@"2"]) {
        return 16;
    }else{
        return 0;
    }
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.index > 0 && ![_vin isEqualToString:@"1"] && ![_vin isEqualToString:@"2"]) {
        UIView *sectionHeadView = [[UIView alloc]init];
        sectionHeadView.backgroundColor = [UIColor whiteColor];
        return sectionHeadView;
    }else{
        return nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
